1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<stdio.h> #include<queue> #include<algorithm> #include<string.h> #include<vector> #define pii pair<int,int> #define piii pair<int ,pii> #define inf 0x3f3f3f3f using namespace std;
const int N=403; int dp[N][N],pre[N][N],num[N][N]; int n,v,m; int a[N]; vector<pii> em[N];
void print(int n,int rest) { if(n<=1)return; print(n-1,pre[n][rest]); printf("%d",num[n][rest]); for(int i=0;i<num[n][rest];i++) printf(" %d",em[n-1][i].second); puts(""); }
int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); scanf("%d%d",&n,&v); for(int i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=1;i<=m;i++) { int l,r,eat; scanf("%d%d%d",&l,&r,&eat); for(int j=l;j<=r;j++) em[j].push_back(pii(eat,i)); } for(int i=1;i<=n;i++) sort(em[i].begin(),em[i].end()); memset(dp,-1,sizeof(dp)); dp[1][0]=0; for(int i=1;i<=n+1;i++) for(int j=0;j<N;j++) if(dp[i][j]!=-1) { int fr=j,now=a[i]; if(fr+now<v)continue; if(fr>=v) fr-=v; else now-=v-fr,fr=0; if(dp[i][j]>dp[i+1][now]) { dp[i+1][now]=dp[i][j]; pre[i+1][now]=j; num[i+1][now]=0; } for(int k=0;k<em[i].size();k++) { int t=em[i][k].first; if(fr+now<t)break; if(fr>=t) fr-=t; else { now-=t-fr; fr=0; } if(dp[i][j]+k+1>dp[i+1][now]) { dp[i+1][now]=dp[i][j]+k+1; pre[i+1][now]=j; num[i+1][now]=k+1; } } } int ansj=0; for(int i=0;i<N;i++) if(dp[n+1][i]>dp[n+1][ansj]) ansj=i; printf("%d\n",dp[n+1][ansj]); print(n+1,ansj); }
|